<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<script>
    //触发事件
    var totalCosts=0;//实收
    var totalCharges=0;//应收
    $('#rcptNo').keydown(function(e){
        if(e.keyCode==13){
            var rcptNo=$("#rcptNo").val();
            if(rcptNo!=null && rcptNo!=''){
                $.ajax({
                       type:'post',
                       url:'${ctx}/fiance/inpSettleMaster/findSettleMaster?rcptNo='+rcptNo,
                       success:function (data){
                               if(data!=null){
                                    var type=data.transactType;
                                   if(type=='2'||type=='3'){//2：作废，3：退费
                                       toastr.warning("对应的数据是退费收据或者作废收据不能进行退费");
                                        return;
                                   }else{
                                       loadSettleData(rcptNo);
                                   }
                               }
                       }
                });
            }else{
                toastr.warning("请输入收据号进行查询");
            }
        }
    });
    //加载数据
    function loadSettleData(rcptNo){
      $("#mainCenterDiv").load('${ctx}/fiance/inpSettleMaster/index?rcptNo='+rcptNo,function(){
          $.ajax({
               type:'post',
               url:'${ctx}/fiance/inpSettleMaster/searchDetails?rcptNo='+rcptNo,
               success:function(data){
                   var trHtml=true;
                   var tableHtml="";
                   tableHtml+= " <thead><tr> " +
                   "<th>收据项目</th> " +
                   "<th>应收金额</th> " +
                   "<th>实收金额</th> " +
                   "<th>收据项目</th> " +
                   "<th>应收金额</th> " +
                   "<th>实收金额</th> " +
                   "<th>收据项目</th> " +
                   "<th>应收金额</th> " +
                   "<th>实收金额</th> " +
                   "</tr> </thead><tbody>";
                   for(var i=0;i<data.length;i++){
                       if(i%3==0 && !trHtml && i!=0){
                           tableHtml+='</tr>';
                           trHtml=true;
                       }
                       if(trHtml){
                           tableHtml+='<tr>';
                           trHtml=false;
                       }
                       tableHtml+='<td>'+data[i].feeClass+'</td> ' +
                       ' <td>'+data[i].costs+'</td> ' +
                       '<td>'+data[i].payments+'</td>'
                       totalCosts += Number(data[i].costs);
                       totalCharges +=  Number(data[i].payments);
                   }

                   totalCharges = Math.floor(totalCharges*100)/100;
                   if(data.length%3!=0){
                       for(var i=0;i<3-data.length%3;i++){
                           tableHtml+="<td></td><td></td><td></td>";
                       }
                   }
                   tableHtml+='</tr>';

                   tableHtml += '<tr><td><font color="red">合计:</font></td>' +
                   '<td><font color="red">'+totalCosts+'</font></td>' +
                   '<td><font color="red">实收:</font></td>' +
                   '<td><font color="red">'+totalCharges+'</font></td><td></td><td></td><td></td><td></td><td></td></tr></tbody>;';
                   $("#list").html(tableHtml);
               }
          });
      });
    }
    //确认取消
    function backSettle(){
        var rcptNo=$("#rcptNo").val();
        if(rcptNo==null || rcptNo==""){
            toastr.warning("请输入收据号");
            return ;
        }
        $.ajax({
             type:'post',
             url:'${ctx}/fiance/inpSettleMaster/cancelSettle?rcptNo='+rcptNo,
             success:function(data){
                 if(data.code=='success'){
                     toastr.success("结算成功");
                     loadDiv('mainCenterDiv','${ctx}/fiance/inpSettleMaster/index');
                     return;
                 }else{
                     toastr.error("收费失败");
                     return;
                 }
             }
        });
    }
</script>
    <div class="content-wrap bg-default">
        <div class="wrapper">
            <div class="row">
                <div class="col-lg-12" id="patientInfo">
                    <section class="panel panel-default">
                        <div class="panel-body" >
                            <form id="searchForm" method="post" class="form-horizontal">
                                <div class="form-group">
                                    <label class="col-sm-1 control-label" for="inpNo">收据号：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" id="rcptNo" name="rcptNo" value="${inpSettleMaster.rcptNo}">
                                    </div>
                                    <label class="col-sm-1 control-label" for="inpNo">住院号：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" id="inpNo" name="visitNo" value="${inpSettleMaster.patVisit.visitNo}">
                                    </div>
                                    <label class="col-sm-1 control-label" for="name">姓名：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="patVisitIndex.name" id="name" value="${inpSettleMaster.patVisit.patVisitIndex.name}">
                                    </div>
                                    <label class="col-sm-1 control-label" for="hospCount">住院天数：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" id="hospCount" name="hospCount" value="${inpSettleMaster.patVisit.zhuyuanTians}">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label" for="admissionDate">入院时间：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" id="admissionDate" name="admissionDate" value="<fmt:formatDate value="${inpSettleMaster.patVisit.admissionDate}" pattern="yyyy-MM-dd"></fmt:formatDate>">
                                    </div>
                                    <label class="col-sm-1 control-label" for="deptAdmissionTo">入院科室：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="deptAdmissionTo" id="deptAdmissionTo" value="${fns:getDeptName(inpSettleMaster.patVisit.deptAdmissionTo,'')}">
                                    </div>
                                    <label class="col-sm-1 control-label" for="enterDeptDate">所在科室：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="enterDeptDate" id="enterDeptDate" value="${fns:getDeptName(inpSettleMaster.patVisit.deptStayed,'')}">
                                    </div>
                                    <label class="col-sm-1 control-label" for="dischargeDate">出院时间：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="dischargeDate" id="dischargeDate" value="<fmt:formatDate value="${inpSettleMaster.patVisit.dischargeDate}" pattern="yyyy-MM-dd"></fmt:formatDate>">
                                    </div>
                                </div>
                            </form>
                        </div>
                    </section>
                </div>
            </div>
            <div class="row">
                <section class="panel">
                    <div class="panel-body">
                    <div class="col-lg-8" id="billDetails">
                        <table class="table table-bordered mg-t" id="list">

                        </table>
                    </div>
                     </div>
                    </section>
            </div>
            <div class="row">
                <div class="col-lg-6">
                    <section class="panel">
                        <div class="panel-body">
                          <table class="table table-bordered mg-t" id="payMents">
                              <thead>
                                <tr>
                                    <th></th>
                                    <th>收</th>
                                    <th>退</th>
                                    <th>小计</th>
                                </tr>
                              </thead>
                              <tbody>
                                <c:forEach items="${inpSettleMaster.inpPaymentsMoneyList}" var="payments">
                                    <tr>
                                        <td>${fns:getDictLabel(payments.moneyType, 'PAY_WAY_DICT', defaultValue)}</td>
                                        <td>${payments.paymentAmount}</td>
                                        <td>${payments.refundedAmount}</td>
                                        <td>${payments.paymentAmount+payments.refundedAmount}</td>
                                        <c:set var="amount" value="${payments.paymentAmount+amount}"></c:set>
                                        <c:set var="refAmount" value="${payments.refundedAmount+refAmount}"></c:set>
                                        <c:set var="total" value="${payments.paymentAmount+payments.refundedAmount+total}"></c:set>
                                    </tr>
                                </c:forEach>
                                 <tr>
                                     <td>合计</td>
                                     <td><c:out value="${amount}"></c:out></td>
                                     <td><c:out value="${refAmount}"></c:out></td>
                                     <td><c:out value="${total}"></c:out></td>
                                 </tr>
                              </tbody>
                          </table>
                        </div>
                    </section>
                 </div>
               <div class="col-lg-6">
                   <section class="panel panel-default">
                       <div class="panel-body">
                           <form method="post" class="form-horizontal">
                           <div class="form-group">
                               <label class="col-sm-3 control-label" for="rcptNo1">收据号：</label>
                               <div class="col-sm-3">
                                   <input type="text" class="form-control" id="rcptNo1" value="${inpSettleMaster.rcptNo}">
                               </div>
                               <label class="col-sm-3 control-label" for="totalCharges">金额：</label>
                               <div class="col-sm-3">
                                   <input type="text" class="form-control"  id="totalCharges" value="${inpSettleMaster.payments}">
                               </div>
                           </div>
                           <div class="form-group">
                               <label class="col-sm-3 control-label" for="date">日期：</label>
                               <div class="col-sm-3">
                                   <input type="text" class="form-control"  id="date" value="<fmt:formatDate value="${inpSettleMaster.createDate}" pattern="yyyy-MM-dd"></fmt:formatDate>">
                               </div>
                               <label class="col-sm-3 control-label" for="operator">收款员：</label>
                               <div class="col-sm-3">
                                   <input type="text" class="form-control"  id="operator" value="${inpSettleMaster.user.name}">
                               </div>
                           </div>
                           <div class="form-group">
                               <label class="col-sm-3 control-label" for="type">类型：</label>
                               <div class="col-sm-3">
                                   <input type="text" class="form-control"  id="type" value="<c:if test="${inpSettleMaster.transactType=='1'}">正常</c:if>">
                               </div>
                           </div>
                           </form>
                           <div class="pull-right">
                               <input type="button" class="btn btn-primary" value="确认" onclick="backSettle();">
                           </div>
                         </div>
                     </section>
               </div>
            </div>

        </div>
    </div>